package com.weimob.mcloud.wechatwork.customer.convertor.tag;

import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.cdp.response.tag.TagAdapterBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.cdp.response.tag.TagGroupAdapterBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.CorpTagGroup;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.Tag;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagGroup;
import com.weimob.mcloud.wechatwork.customer.service.user.enums.TagSourceEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagGroupBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagItemBO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/11/11 3:02 下午
 */
public class TagGroupConvert {

    public static List<String> toCdpTagGroupIds(List<TagGroup> tagGroups) {
        List<String> cdpTagGroupIds = new ArrayList<>();
        for (TagGroup tagGroup : tagGroups) {
            cdpTagGroupIds.add(tagGroup.getCdpTagGroupId());
        }

        return cdpTagGroupIds;
    }

    public static Set<String> toExternalTagGroupIds(List<TagGroup> tagGroups) {
        if (CollectionUtils.isEmpty(tagGroups)) {
            return new HashSet<>();
        }

        Set<String> externalTagGroupIds = new HashSet<>();
        for (TagGroup tagGroup : tagGroups) {
            externalTagGroupIds.add(tagGroup.getExternalTagGroupId());
        }

        return externalTagGroupIds;
    }

    public static List<String> toCdpTagIds(List<TagGroupBO> customerTagList) {
        if (CollectionUtils.isEmpty(customerTagList)) {
            return new ArrayList<>();
        }

        List<String> cdpTagIds = new ArrayList<>();
        for (TagGroupBO tagGroupBO : customerTagList) {
            List<TagBO> tagBOList = tagGroupBO.getTagBOList();
            if (CollectionUtils.isEmpty(tagBOList)) {
                continue;
            }


            for (TagBO tagBO : tagBOList) {
                cdpTagIds.add(tagBO.getCdpTagId());
            }
        }

        return cdpTagIds;
    }


    public static TagItemBO toTagItemBO(List<TagGroupBO> tagGroupBOS) {
        if (CollectionUtils.isEmpty(tagGroupBOS)) {
            return null;
        }

        List<TagGroupBO> corpTagGroup = new ArrayList<>();

        List<TagGroupBO> weimobTagGroup = new ArrayList<>();

        for (TagGroupBO tagGroupBO : tagGroupBOS) {
            if (TagSourceEnum.WECHAT.getSource().equals(tagGroupBO.getTagSource())) {
                corpTagGroup.add(tagGroupBO);
            } else {
                weimobTagGroup.add(tagGroupBO);
            }
        }

        TagItemBO tagItemBO = new TagItemBO();
        tagItemBO.setCorpTagGroup(corpTagGroup);
        tagItemBO.setWeimobTagGroup(weimobTagGroup);

        return tagItemBO;

    }

    public static List<TagBO> toTagBO(List<TagGroupBO> tagGroupBOS) {
        if (CollectionUtils.isEmpty(tagGroupBOS)) {
            return new ArrayList<>();
        }

        List<TagBO> tagBOList = new ArrayList<>();
        for (TagGroupBO tagGroupBO : tagGroupBOS) {
            List<TagBO> tagBOListRes = tagGroupBO.getTagBOList();
            if (CollectionUtils.isEmpty(tagBOListRes)) {
                continue;
            }

            for (TagBO tagBO : tagBOListRes) {

                TagBO tagBORes = new TagBO();
                TagGroupBO tagGroupBORes = new TagGroupBO();
                tagGroupBORes.setCdpTagGroupId(tagGroupBO.getCdpTagGroupId());
                tagGroupBORes.setTagGroupName(tagGroupBO.getTagGroupName());
                tagGroupBORes.setTagSource(tagGroupBO.getTagSource());

                tagBORes.setCdpTagId(tagBO.getCdpTagId());
                tagBORes.setTagName(tagBO.getTagName());
                tagBORes.setTagSource(tagBO.getTagSource());
                tagBORes.setTagGroupBO(tagGroupBORes);
                tagBORes.setUse(tagBO.getUse());
                tagBORes.setTagValue(tagBO.getTagValue());
                tagBORes.setTagType(tagBO.getTagType());

                tagBOList.add(tagBORes);
            }

        }

        return tagBOList;
    }

    public static void fillTagSource(List<TagGroupBO> tagGroupBOS, List<String> corpTagGroupIds) {
        if (CollectionUtils.isEmpty(tagGroupBOS) || CollectionUtils.isEmpty(corpTagGroupIds)) {
            return;
        }

        //标签来源处理
        for (TagGroupBO tagGroupBO : tagGroupBOS) {
            if (!corpTagGroupIds.contains(tagGroupBO.getCdpTagGroupId())) {
                continue;
            }

            tagGroupBO.setTagSource(TagSourceEnum.WECHAT.getSource());

            List<TagBO> tagBOList = tagGroupBO.getTagBOList();
            if (CollectionUtils.isEmpty(tagBOList)) {
                continue;
            }

            for (TagBO tagBO : tagBOList) {
                tagBO.setTagSource(TagSourceEnum.WECHAT.getSource());
            }
        }
    }

    public static void fillCustomerTagUse(List<TagGroupBO> tagGroupBOS, List<String> customerTagIds) {
        if (CollectionUtils.isEmpty(tagGroupBOS)) {
            return;
        }

        //标签来源处理
        for (TagGroupBO tagGroupBO : tagGroupBOS) {

            List<TagBO> tagBOList = tagGroupBO.getTagBOList();
            if (CollectionUtils.isEmpty(tagBOList)) {
                continue;
            }

            for (TagBO tagBO : tagBOList) {
                if (CollectionUtils.isNotEmpty(customerTagIds) && customerTagIds.contains(tagBO.getCdpTagId())) {
                    tagBO.setUse(true);
                } else {
                    tagBO.setUse(false);
                }
            }
        }
    }

    public static List<TagGroupBO> toTagGroupBOFromAdapter(List<TagGroupAdapterBO> tagGroupAdapterBOS) {

        List<TagGroupBO> tagGroupBOList = new ArrayList<>();
        for (TagGroupAdapterBO tagGroupAdapterBO : tagGroupAdapterBOS) {
            List<TagAdapterBO> tagAdapterBOS = tagGroupAdapterBO.getTagBOS();
            if (CollectionUtils.isEmpty(tagAdapterBOS)) {
                continue;
            }
            TagGroupBO tagGroupBO = new TagGroupBO();
            tagGroupBO.setCdpTagGroupId(tagGroupAdapterBO.getCdpTagGroupId());
            tagGroupBO.setTagGroupName(tagGroupAdapterBO.getTagGroupName());
            tagGroupBO.setTagSource(tagGroupAdapterBO.getTagSource());
            tagGroupBO.setTagType(tagGroupAdapterBO.getTagType());

            List<TagBO> tagBOList = new ArrayList<>();
            for (TagAdapterBO tagAdapterBO : tagAdapterBOS) {
                TagBO tagBO = new TagBO();
                tagBO.setCdpTagId(tagAdapterBO.getCdpTagId());
                tagBO.setTagName(tagAdapterBO.getTagName());
                tagBO.setTagSource(tagAdapterBO.getTagSource());
                tagBO.setTagType(tagAdapterBO.getTagType());
                tagBO.setTagValue(tagAdapterBO.getTagValue());
                tagBOList.add(tagBO);
            }

            tagGroupBO.setTagBOList(tagBOList);

            tagGroupBOList.add(tagGroupBO);
        }

        return tagGroupBOList;
    }

    public static List<TagGroupBO> toTagGroupBOSFromCore(List<TagGroup> tagGroups) {
        if (CollectionUtils.isEmpty(tagGroups)) {
            return new ArrayList<>();
        }

        List<TagGroupBO> tagGroupBOList = new ArrayList<>();
        for (TagGroup tagGroup : tagGroups) {
            TagGroupBO tagGroupBO = toTagGroupBOFromAdapter(tagGroup);
            tagGroupBOList.add(tagGroupBO);
        }


        return tagGroupBOList;
    }

    public static TagGroupBO toTagGroupBOFromAdapter(TagGroup tagGroup) {

        TagGroupBO tagGroupBO = new TagGroupBO();
        tagGroupBO.setExternalTagGroupId(tagGroup.getExternalTagGroupId());
        tagGroupBO.setCdpTagGroupId(tagGroup.getCdpTagGroupId());
        tagGroupBO.setTagSource(tagGroup.getTagSource());
        tagGroupBO.setTagGroupName(tagGroup.getTagGroupName());

        List<Tag> tagList = tagGroup.getTagList();
        if (Objects.nonNull(tagList)) {
            List<TagBO> tagBOList = TagConvert.toTagBOS(tagList);
            tagGroupBO.setTagBOList(tagBOList);
        }

        return tagGroupBO;
    }

    public static Set<String> toCorpTagGroupIds(List<CorpTagGroup> corpTagGroups) {
        if (CollectionUtils.isEmpty(corpTagGroups)) {
            return new HashSet<>();
        }

        Set<String> corpTagGroupIds = new HashSet<>();
        for (CorpTagGroup corpTagGroup : corpTagGroups) {
            corpTagGroupIds.add(corpTagGroup.getGroupId());
        }

        return corpTagGroupIds;
    }

    public static Map<String, String> toGroupIdAndNameMap(List<TagGroup> tagGroups) {
        if (CollectionUtils.isEmpty(tagGroups)) {
            return new HashMap<>();
        }

        Map<String, String> groupIdAndNameMap = new HashMap<>();
        for (TagGroup tagGroup : tagGroups) {
            groupIdAndNameMap.put(tagGroup.getExternalTagGroupId(), tagGroup.getTagGroupName());
        }

        return groupIdAndNameMap;
    }

    public static Set<String> toCorpGroupIds(List<TagGroup> tagGroups) {
        if (CollectionUtils.isEmpty(tagGroups)) {
            return new HashSet<>();
        }

        Set<String> corpTagGroupIds = new HashSet<>();
        for (TagGroup tagGroup : tagGroups) {
            corpTagGroupIds.add(tagGroup.getExternalTagGroupId());
        }

        return corpTagGroupIds;
    }
}
